<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @search-reset="searchReset"
        @refresh-change="refreshChange"
        :permission="permissionList"
        :before-open="handleBeforeOpen"
        @selection-change="selectionChange"
        >
        <template slot="menuLeft">
          <el-button type="primary" size="small" @click="handleExport">导出
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { dataExport } from '@/api/subwayepm/swexdevicefault.js'
import baseCrud from '@/mixins/base-crud'
import {dateFormat} from '@/util/date'
import { handleExcelResponse } from "@/util/util";
import { fetchList } from '@/api/subwayepm/swexdevicefault'
import { tableOption, startDate , endDate } from '@/const/crud/subwayepm/swexdevicefault'

export default {
  name: 'swexdevicefault',
  mixins: [baseCrud('subwayepm/swexdevicefault')],
  data() {
    return {
      selection: [],
      searchForm: {
        faultTimeDto: [startDate, endDate]
      },
      tableLoading: false,
      tableOption: tableOption,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
    }
  },
  computed: {
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.epm_sw_ex_device_fault_add, false),
        delBtn: this.vaildData(this.permissions.epm_sw_ex_device_fault_del, false),
        editBtn: this.vaildData(this.permissions.epm_sw_ex_device_fault_edit, false)
      }
    }
  },
  methods: {
    getList (page, params) {
      this.tableLoading = true
      const [startDate , endDate] = this.searchForm.faultTimeDto || [];
      fetchList(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, {startDate,endDate}, this.searchForm)).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    async searchChange (form, done) {
      this.searchForm = { ...form, ...this.extraParam };
      try {
        await this.getList(this.page, this.searchForm);
      } finally {
        done();
      }
    },
    refreshChange () {
      this.getList(this.page)
    },
    searchReset () {
      this.page.currentPage = 1
      this.getList(this.page);
    },
    handleBeforeOpen(done, type) {
      if (type == 'add') {
        setTimeout(() => {
          this.form.faultTime = dateFormat(new Date())
          this.form.handlePersonName = this.$store.getters.userInfo.nikename;
          this.form.handlePersonCode = this.$store.getters.userInfo.userCode;
        })
      }
      done()
    },

    selectionChange (list) {
      this.selection = list.map(l => l.id);
    },

    //导出
    async handleExport () {
      let loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      })
      try {
        if(this.selection.length>0){
          const res = await dataExport({ ids: this.selection });
          handleExcelResponse(res);
        }else{
          const res = await dataExport();
          handleExcelResponse(res);
        }
      } finally {
        loading.close();
      }
    },
  }
}
</script>
